package com.acegear.horizon.domain.services;

import com.acegear.horizon.domain.models.jpa.PhoneVerify;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * Created by wangsike on 2016/12/16.
 */
@Service
public class SMSService {

    @Value("${horizon.sms.server}")
    private String server;

    @Value("${horizon.sms.verifyUrl}")
    private String verifyUrl;

    public Boolean sendVerifySMS(PhoneVerify phoneVerify) {
        RestTemplate restTemplate = new RestTemplate();
        String url = UriComponentsBuilder
                .fromHttpUrl(server + verifyUrl)
                .queryParam("phone", phoneVerify.getPhone())
                .queryParam("param1", phoneVerify.getCode())
                .build().toUriString();
        SMSResponse response = restTemplate.getForObject(url, SMSResponse.class);
        return response.getCode() == 0;
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class SMSResponse {
    private Integer code;
    private String msg;
    private Integer count;

    public SMSResponse() {

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }
}
